import Cookies from 'js-cookie';
import util from '@/libs/util';

export default {
    data () {
        return {
            baseuser: JSON.parse(Cookies.get('baseUser')),
            formData: {
                maxApplyNo: 0,
                defaultApplyLimitedTimed: 0,
                defaultApplyLimitedTimeh: 0,
                defaultApplyLimitedTimem: 0,
                defaultMessageApplyLimitedTimed: 0,
                defaultMessageApplyLimitedTimeh: 0,
                defaultMessageApplyLimitedTimem: 0,
                ifWxPay:0,
                ifAliPay:0
            },
            options: {
                shortcuts: [{
                    text: '今天',
                    value () {
                        return new Date();
                    }
                }]
            },
            selectYesOrNo: [{id: 1, name: '是'}, {id: 2, name: '否'}],
            applyTypeList: [],
            applyLimitedTypeList: []
        };
    },
    mounted () {
        this.loadDictionaries();
        this.loadList();
    },
    methods: {
        loadDictionaries () {
            util.ajax.post('/base/datadictionary/findByCode', util.toFormData({
                CODE: 'SIGNUP_TYPE'
            })).then(res => {
                this.applyTypeList = res.data.data;
            });
            util.ajax.post('/base/datadictionary/findByCode', util.toFormData({
                CODE: 'IS_WEEKLY'
            })).then(res => {
                this.applyLimitedTypeList = res.data.data;
            });
        },
        loadList () {
            util.ajax.post('base/config/list', util.toFormData({
                id: this.baseuser.orgId
            })).then(res => {
                this.formData = res.data.data;
                var str = this.formData.defaultApplyLimitedTime;
                var arr = str.split('@');
                this.formData.defaultApplyLimitedTimed = arr[0]
                this.formData.defaultApplyLimitedTimeh = arr[1]
                this.formData.defaultApplyLimitedTimem = arr[2]
                var mes = this.formData.defaultMessageApplyLimitedTime;
                var arry = mes.split('@');
                this.formData.defaultMessageApplyLimitedTimed = arry[0]
                this.formData.defaultMessageApplyLimitedTimeh = arry[1]
                this.formData.defaultMessageApplyLimitedTimem = arry[2]
                this.formData.applyStartTime = util.getDate(this.formData.applyStartTime, 'time');
                this.formData.applyEndTime = util.getDate(this.formData.applyEndTime, 'time');
            });
        },
        refresh () {
        },
        save () {
            let boo = false;
            let content = '';
            let formData = [];
            for (var key in this.formData) {
                formData[key] = this.formData[key];
            }
            if (!formData.applyStartTime) {
                content = '“报名开始时间”不能为空！';
                boo = true;
            }
            if (!formData.applyEndTime) {
                content = '“报名结束时间”不能为空！';
                boo = true;
            }
            if (formData.defaultApplyLimitedTimed + formData.defaultApplyLimitedTimeh + formData.defaultApplyLimitedTimem === 0) {
                content = '请输入“默认教务报名有效缴费时间”';
                boo = true;
            }
            if (formData.defaultMessageApplyLimitedTimed + formData.defaultMessageApplyLimitedTimeh + formData.defaultMessageApplyLimitedTimem === 0) {
                content = '请输入“默认网上（短信）报名有效缴费时间”';
                boo = true;
            }
            if (boo) {
                this.$Modal.error({
                    content: content
                });
                return;
            }
            // formData['applyStartTimes'] = this.formData['applyStartTime'];
            // formData['applyEndTimes'] = this.formData['applyEndTime'];
            // this.formData['applyStartTime'] = new Date();
            // this.formData['applyEndTime'] = new Date();
            formData.defaultApplyLimitedTime = this.formData.defaultApplyLimitedTimed + '@' + this.formData.defaultApplyLimitedTimeh + '@' + this.formData.defaultApplyLimitedTimem;
            formData.defaultMessageApplyLimitedTime = this.formData.defaultMessageApplyLimitedTimed + '@' + this.formData.defaultMessageApplyLimitedTimeh + '@' + this.formData.defaultMessageApplyLimitedTimem;
            util.ajax.post('base/config/update', util.toFormData(formData)).then(res => {
                if (res.data.message === 'SUCCESS') {
                    this.$Message.success('保存成功！');
                    this.loadList();
                } else {
                    this.$Message.warning(res.data.message || '未知错误！');
                }
            });
        }
    }
}